package com.gongpingjialoan.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.DealerCat;
import com.gongpingjialoan.utility.Utils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/13.
 */
public class DelearTabView extends LinearLayout implements View.OnClickListener {
    Context mContext;

    View brandV, priceV, orderV, ageV;

    TextView brandT, priceT, orderT, ageT;

    SortPop yearPop, pricePop, sortPop;

    List<DealerCat> agelist, pricelist, orderlist;

    private Resources mResources;

    LinearLayout tabV;

    Drawable drawableDown, drawNull;

    BrandPop brandPop;
    OnTabSelectListener onTabSelectListener;


    public DelearTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mResources = getResources();
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.tab_dealer, this);

        brandV = findViewById(R.id.brand_layout);
        priceV = findViewById(R.id.price_layout);
        orderV = findViewById(R.id.order_layout);
        ageV = findViewById(R.id.age_layout);
        brandV.setOnClickListener(this);
        priceV.setOnClickListener(this);
        orderV.setOnClickListener(this);
        ageV.setOnClickListener(this);


        brandT = (TextView) findViewById(R.id.brand);
        priceT = (TextView) findViewById(R.id.price);
        orderT = (TextView) findViewById(R.id.order);
        ageT = (TextView) findViewById(R.id.age);
        sortPop = new SortPop(mContext);
        sortPop.setOnReslutClickListener(new SortPop.OnReslutClickListener() {
            @Override
            public void result(DealerCat dealerCat) {
                if (onTabSelectListener != null) {
                    onTabSelectListener.sort(dealerCat);
                }
                if (!TextUtils.isEmpty(dealerCat.getValue())) {
                    setDrawRight(orderT, drawableDown, R.color.nav_bg_color);
                } else {
                    setDrawRight(orderT, drawNull, R.color.text_details_color);
                }
            }
        });


        yearPop = new SortPop(mContext);
        yearPop.setOnReslutClickListener(new SortPop.OnReslutClickListener() {
            @Override
            public void result(DealerCat dealerCat) {
                if (onTabSelectListener != null) {
                    onTabSelectListener.sort(dealerCat);
                }

                if (!TextUtils.isEmpty(dealerCat.getValue())) {
                    setDrawRight(ageT, drawableDown, R.color.nav_bg_color);
                } else {
                    setDrawRight(ageT, drawNull, R.color.text_details_color);
                }
            }
        });


        pricePop = new SortPop(mContext);
        pricePop.setOnReslutClickListener(new SortPop.OnReslutClickListener() {
            @Override
            public void result(DealerCat dealerCat) {
                if (onTabSelectListener != null) {
                    onTabSelectListener.sort(dealerCat);
                }
                if (!TextUtils.isEmpty(dealerCat.getValue())) {
                    setDrawRight(priceT, drawableDown, R.color.nav_bg_color);

                } else {
                    setDrawRight(priceT, drawNull, R.color.text_details_color);
                }


            }
        });
        tabV = (LinearLayout) findViewById(R.id.tab_layout);
        drawableDown = mResources.getDrawable(R.drawable.sort_focus_new);
        drawNull = mResources.getDrawable(R.drawable.sort_normal_new);
        brandPop = new BrandPop(mContext);
        brandPop.setOnReslutClickListener(new BrandPop.OnReslutClickListener() {
            @Override
            public void result(String brandName, String brandSlug, String modelname, String modelslug) {
                if (onTabSelectListener != null) {
                    onTabSelectListener.brand(brandName, brandSlug, modelname, modelslug);
                }
                if (!TextUtils.isEmpty(brandName)) {
                    setDrawRight(brandT, drawableDown, R.color.nav_bg_color);
                } else {
                    setDrawRight(brandT, drawNull, R.color.text_details_color);
                }
            }
        });
        initData();
    }

    private void initData() {
        agelist = new ArrayList<>();
        pricelist = new ArrayList<>();
        orderlist = new ArrayList<>();
        agelist.add(new DealerCat("不限车龄", "", "year"));
        agelist.add(new DealerCat("1年以内", Utils.getCarYear("1年以内"), "year"));
        agelist.add(new DealerCat("1-3年", Utils.getCarYear("1-3年"), "year"));
        agelist.add(new DealerCat("3-5年", Utils.getCarYear("3-5年"), "year"));
        agelist.add(new DealerCat("5年以上", Utils.getCarYear("5年以上"), "year"));

        pricelist.add(new DealerCat("不限价格", "", "price"));
        pricelist.add(new DealerCat("3万以内", "0-3", "price"));
        pricelist.add(new DealerCat("3-5万", "3-5", "price"));
        pricelist.add(new DealerCat("5-10万", "5-10", "price"));
        pricelist.add(new DealerCat("10-15万", "10-15", "price"));

        orderlist.add(new DealerCat("默认排序", "", "sort"));
        orderlist.add(new DealerCat("价格从低到高", "price", "sort"));
        orderlist.add(new DealerCat("价格从高到低", "-price", "sort"));
        orderlist.add(new DealerCat("上牌时间最短", "-age", "sort"));
        orderlist.add(new DealerCat("上牌时间最长", "age", "sort"));
        orderlist.add(new DealerCat("发布时间最新", "-pub_time", "sort"));
        orderlist.add(new DealerCat("推荐指数最高", "-gpj_index", "sort"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.brand_layout:
                brandPop.show(v);
                break;
            case R.id.age_layout:
                yearPop.setData(agelist);
                yearPop.setLabel(ageT);
                yearPop.show(this);
                break;

            case R.id.price_layout:
                pricePop.setData(pricelist);
                pricePop.setLabel(priceT);
                pricePop.show(this);
                break;

            case R.id.order_layout:
                sortPop.setData(orderlist);
                sortPop.setLabel(orderT);

                sortPop.show(this);
                break;

        }

    }


    public void setData(JSONArray jsa, String count) {
        brandPop.setData(jsa, count);

    }





    private void setDrawRight(TextView textView, Drawable drawable, int textcolor) {


        textView.setTextColor(mResources.getColor(textcolor));
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);

    }

    public OnTabSelectListener getOnTabSelectListener() {
        return onTabSelectListener;
    }

    public void setOnTabSelectListener(OnTabSelectListener onTabSelectListener) {
        this.onTabSelectListener = onTabSelectListener;
    }

    public interface OnTabSelectListener {
        void sort(DealerCat cat);

        void brand(String brandName, String brandSlug, String modelname, String modelsulg);
    }

}
